/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package module;

import wargame.entity.*;
import wargame.dao.*;

/**
 *
 * @author Alvin Chua
 */
public class GenerateProject {

    public static Project createProject(Company company, int currentTurn) {
        Project newProject = new Project();

        //ProjectDAO pDAO = new ProjectDAO();

        newProject.setOffered_by(company.getCompany_id());

        //generate random numbers to multiply the company's preference to get the number of hours
        double amount = Math.random() * 100;
        double amount2 = Math.random() * 100;
        double amount3 = Math.random() * 100;
        double amount4 = Math.random() * 100;
        double amount5 = Math.random() * 100;
        double amount6 = Math.random() * 100;
        double amount7 = Math.random() * 100;
        double amount8 = Math.random() * 100;

        int trackRecordPoint = 0;

        //getting the company's preference
        int dotNet = company.getPreferred_skill_dot_net();
        int cSharp = company.getPreferred_skill_c_sharp();
        int jsp = company.getPreferred_skill_jsp();
        int php = company.getPreferred_skill_php();
        int ruby = company.getPreferred_skill_ruby();
        int mysql = company.getPreferred_skill_mysql();
        int mssql = company.getPreferred_skill_mssql();
        int oracle = company.getPreferred_skill_oracle();

        //generating man hours for the project
        if (dotNet > 0) {
            double tempAmt = amount * dotNet;
            newProject.setMan_hours_dot_net((int) tempAmt);
            trackRecordPoint += (int) tempAmt;
        }

        if (cSharp > 0) {
            double tempAmt = amount2 * cSharp;
            newProject.setMan_hours_c_sharp((int) tempAmt);
            trackRecordPoint += (int) tempAmt;
        }

        if (jsp > 0) {
            double tempAmt = amount3 * jsp;
            newProject.setMan_hours_jsp((int) tempAmt);
            trackRecordPoint += (int) tempAmt;
        }

        if (php > 0) {
            double tempAmt = amount4 * php;
            newProject.setMan_hours_php((int) tempAmt);
            trackRecordPoint += (int) tempAmt;
        }

        if (ruby > 0) {
            double tempAmt = amount5 * ruby;
            newProject.setMan_hours_ruby((int) tempAmt);
            trackRecordPoint += (int) tempAmt;
        }

        if (mysql > 0) {
            double tempAmt = amount6 * mysql;
            newProject.setMan_hours_mysql((int) tempAmt);
            trackRecordPoint += (int) tempAmt;
        }

        if (mssql > 0) {
            double tempAmt = amount7 * mssql;
            newProject.setMan_hours_mssql((int) tempAmt);
            trackRecordPoint += (int) tempAmt;
        }

        if (oracle > 0) {
            double tempAmt = amount8 * oracle;
            newProject.setMan_hours_oracle((int) tempAmt);
            trackRecordPoint += (int) tempAmt;
        }

        newProject.setTrack_record_point(trackRecordPoint);

        //formula is fexible, for now I determine it as the total number of hours multiply by 1000
        int max_bid = trackRecordPoint * GlobalConstant.MAX_BID_MULTIPLIER;

        //max bid will depend on the current economic index and the sensitivity of the company
        //! this one confirm 0
        Economic economicIndexNow = EconomicDAO.retrieveEconomic(currentTurn);
        Economic economicIndexPrevious = EconomicDAO.retrieveEconomic(currentTurn - 1);
        double change  = 0;

        if (currentTurn != 0) {
            change = economicIndexNow.getEconomic_index() - economicIndexPrevious.getEconomic_index();
        }

        double changeRate = 1;
        double changeIndex = change / company.getEconomic_sensitivity();
        if(changeIndex < -5){
            return null;
        } else {
            changeRate += changeIndex * 0.1 ;
        }
         
        double adjusted_max_bid = max_bid * (economicIndexNow.getEconomic_index()/10) *  changeRate;
        newProject.setMax_bid((int) adjusted_max_bid);
        //newProject.setMax_bid((int) (max_bid));

        //i'll take the case where having the highest lvl (lvl 3) an employee can complete 150 hrs per turn
        int due_turn = (trackRecordPoint / 150) + currentTurn + 2;

        newProject.setDue_turn(due_turn);



        int listing_turn = 1;

        // If the project is big enough
        if (trackRecordPoint > 150) {
            Double rand = Math.random();
            // Lets give it a 50 % chance to be listed twice
            if (rand > 0.5) {
                listing_turn = 2;
            }
        }

        //old listing method
        //int listing_turn = due_turn/3;
        //listing_turn = (listing_turn <1)? 1 : listing_turn;
        newProject.setListing_turn_remaining(listing_turn);

        //place into database
        ProjectDAO.insertProject(newProject);

        return newProject;
    }

    public static void submitProject(Project project, int currentTurn, Player player) {
        //check if disable is true

        //create transaction
        TransactionDAO tDAO = new TransactionDAO();
        Transaction newTransaction = new Transaction();

        newTransaction.setPlayer_id(player.getPlayer_id());
        newTransaction.setTurn(currentTurn);
        newTransaction.setDescription("submit project:" + project.getProject_id());

        int penaltyAmt = 0;
        int balance = player.getBalance();
        int trackRecord = player.getTrack_record_points();

        //calculate the penalty amt if the project is submitted late
        if (project.getDue_turn() < currentTurn) {
            int penaltyTurns = currentTurn - project.getDue_turn();
            penaltyAmt = penaltyTurns * (1 / 5) * project.getBidded_price();
        }

        //find the final balance of the player
        balance = balance + project.getBidded_price() - penaltyAmt;

        //adds track record or deduct depending on status
        if (penaltyAmt > 0) {
            trackRecord -= project.getTrack_record_point();
        } else {
            trackRecord += project.getTrack_record_point();
        }

        player.setBalance(balance);
        player.setTrack_record_points(trackRecord);

        //set credit amount
        newTransaction.setCredit_amount(project.getBidded_price() - penaltyAmt);

        PlayerDAO updatePlayer = new PlayerDAO();

        //update player records
        updatePlayer.updatePlayerRecords(player);

        //update project status
        project.setSubmitted(1);

        ProjectDAO projectStatus = new ProjectDAO();

        projectStatus.updateProjectStatus(project);

        //add transaction
        tDAO.insertTransaction(newTransaction);

        //add into game log
        GameLog gl = new GameLog(player.getPlayer_id(), "Project: " + project.getProject_id() + ",submitted by: " + player.getPlayer_name(), currentTurn);
        GameLogDAO.insertGameLog(gl);

    }
    //outsource project (dont think need it)
}
